﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Requests;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Groups;
    using SharingCenter.Web.Classes.Visits;
    using System.Xml.Linq;
    using SharingCenter.Web.Shared;
    public partial class ViewGroup : GroupsPageBase
    {
        protected bool isMember = false;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                RedirectToNoRightsPage();
            }
            ParseGroupIdFromQueryString();
            HasRights(groupId, Profile.UserId);

            isMember = member != null;
            LinqDataSource1.Selected += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Selected);
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            lvSidebar.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvSidebar_ItemDataBound);
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
            lvSidebar.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvSidebar_ItemCommand);
            UpdateVisitCount();
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            List<Sc_Group> l = new List<Sc_Group>();
            scDataContext cxt = new scDataContext();
            group = scDataContext.Instance.Sc_Groups.SingleOrDefault(d => d.GroupId == groupId);
            l.Add(group);
            e.Result = l;
            lvSidebar.DataSource = l;
            lvSidebar.DataBind();
        }

        void lvSidebar_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "JoinRequest":
                    SendJoinRequest(ref e);
                    break;
                default: break;
            }
        }

        private void SendJoinRequest(ref ListViewCommandEventArgs e)
        {
            List<Guid> admin = GroupOperations.GetGroupAdminUserList(groupId);
            XElement context = new XElement("GroupRequestContext",
                new XAttribute("XsltFolderInMod", "Groups"),
                new XElement("GroupId", groupId),
                new XElement("RequestorUserId", Profile.UserId),
                new XElement("GroupName", group.GroupName),
                new XElement("Comment", ((TextBox)e.Item.FindControl("txtJoinComment")).Text)
                );
            RequestOperations.AddRequest(EntityType.Group, groupId, Profile.UserId, admin, context);
            ((MultiView)e.Item.FindControl("mvRequestJoin")).ActiveViewIndex = 1;
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            BindThreadList(ref e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<Sc_Group> tm = new List<Sc_Group>();
            //tm.Add(GroupOperations.GetGroupByGroupId(this.groupId));
            //ListView1.DataSource = tm;
            //ListView1.DataBind();
            ltrTitle.Text = group.GroupName;
        }

        protected void SiteEventList1_Load(object sender, EventArgs e)
        {
            SiteEventList s = (SiteEventList)sender;
            s.GroupId = groupId;
        }

        private void BindThreadList(ref ListViewItemEventArgs e)
        {
            ListView threadList = (ListView)e.Item.FindControl("lvThreadsList");
            ListView eventList = (ListView)e.Item.FindControl("lvEventList");
            threadList.DataSource = scDataContext.Instance.GetGroupThreads(groupId, 10);
            eventList.DataSource = scDataContext.Instance.GetGroupEvents(groupId, 5);
            threadList.DataBind();
            eventList.DataBind();
        }

        private void UpdateVisitCount()
        {
            VisitOperations.AddVisit(EntityType.Group, groupId, Profile.UserId);
        }

        void lvSidebar_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            BindAdminList(ref e);           
        }

        void BindAdminList(ref ListViewItemEventArgs e)
        {
            Sc_Group item = (Sc_Group)((ListViewDataItem)e.Item).DataItem;
            Page.Title = item.GroupName;
            ListView adminList = (ListView)e.Item.FindControl("lvAdminList");
            adminList.DataSource = GroupOperations.GetGroupAdminList(groupId);
            adminList.DataBind();
        }

        void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            
        }

        //protected void AdminAvatar_PreRender(object sender, EventArgs e)
        //{
        //    lvSidebar.Items[0].FindControl("phAdmin").Visible = member.AdminRole == GroupRole.Owner || member.AdminRole == GroupRole.GroupAdmin;
        //}

        protected void pnJoinGroup_Load(object sender, EventArgs e)
        {
            ((Control)sender).Visible = !isMember;
        }

        protected void ThreadLastUpdate_PreRender(object sender, EventArgs e)
        {
            Label s = (Label)sender;
            s.Text = Utility.FriendlyDateTime(s.Text, TimeDisplayEnum.LiteralHours, false);
        }

        protected void EnterThreadLink_PreRender(object sender, EventArgs e)
        {
            HyperLink s = (HyperLink)sender;
            s.NavigateUrl += groupId;
        }

        protected void DateFormat(object sender, EventArgs e)
        {
            Label dateLabel = (Label)sender;
            dateLabel.Text = Utility.FriendlyDateTime(dateLabel.Text, TimeDisplayEnum.AmPmOnly, true);
        }

        protected void NavigateUrlAddGroupId(object sender, EventArgs e)
        {
            HyperLink lnk = (HyperLink)sender;
            lnk.NavigateUrl += groupId;
        }

        protected void mvComment_PreRender(object sender, EventArgs e)
        {
            ((MultiView)sender).ActiveViewIndex = isMember ? 0 : 1;
        }
    }
}
